package com.woniu.car.realm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.car.bean.Login;
import com.woniu.car.service.ILoginService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private ILoginService loginService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        System.out.println(username);
        QueryWrapper<Login> qw = new QueryWrapper<>();
        qw.eq("username",username);
        // 获取数据库对象
        Login user = loginService.getOne(qw);
        System.out.println(user);
        //获取盐值
        ByteSource salt = ByteSource.Util.bytes(user.getSalt());
        AuthenticationInfo authenticationInfo =new SimpleAuthenticationInfo(
                user,
                user.getPassword(),
                salt,
                getName()
        );

        return authenticationInfo;
    }
}
